home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / buttons.c < prev    next >
C/C++ Source or Header  |  1996-07-31  |  5KB  |  194 lines

  1. #include <ctype.h>
  2. #include <limits.h>
  3. #include "diadef.h"
  4. #include "dialog.h"
  5. #include "internal.h"
  6. #include "dialog.m"
  7.  
  8. /*
  9.     Evaluate the horizontal space needed to draw the buttons
  10. */
  11. PUBLIC int BUTTONS_INFO::evalwidth()
  12. {
  13.     int ret = 0;
  14.     for (int i=0; i<nb; i++){
  15.         /* add 2 for the < > on each side */
  16.         /* add 2 for some space around the buttons */
  17.         ret += strlen(tb[i]) + 2+2;
  18.     }
  19.     if (!helpfile.is_empty()) ret += 6 + 2 + 2;
  20.     return ret;
  21. }
  22. PUBLIC BUTTONS_INFO::BUTTONS_INFO()
  23. {
  24.     nb = 0;
  25. }
  26.  
  27. PUBLIC void BUTTONS_INFO::set(
  28.     int _but_options,    // MENUBUT_xxxx | MENUBUT_xxxx | ...
  29.     const char *_helpfile)
  30. {
  31.     nb = 0;
  32.     but_options = _but_options;
  33.     /*
  34.         Not all buttons combination are meaningful. This
  35.         explain why several buttons may share the same
  36.         but_ret value.
  37.     */
  38.     static struct BUTTON_ALL{
  39.         short but_id;
  40.         MENU_STATUS but_ret;    // Returned value when this button
  41.                     // is picked
  42.         const char *str;
  43.     } tbl[]={
  44.         { MENUBUT_YES,    MENU_YES,    MSG_U(B_YES,"Yes")    },
  45.         { MENUBUT_NO,    MENU_NO,    MSG_U(B_NO,"No")    },
  46.         { MENUBUT_OK,    MENU_OK,    MSG_U(B_OK,"Ok")    },
  47.         { MENUBUT_ACCEPT,    MENU_ACCEPT,    MSG_U(B_ACCEPT,"Accept")    },
  48.         { MENUBUT_CANCEL,    MENU_CANCEL,    MSG_U(B_CANCEL,"Cancel")    },
  49.         { MENUBUT_QUIT,        MENU_QUIT,    MSG_U(B_QUIT,"Quit")    },
  50.         { MENUBUT_SAVE,        MENU_SAVE,    MSG_U(B_SAVE,"Save")    },
  51.         { MENUBUT_ADD,        MENU_ADD,    MSG_U(B_ADD,"Add")    },
  52.         { MENUBUT_DEL,        MENU_DEL,    MSG_U(B_DEL,"Del")    },
  53.         { MENUBUT_INS,        MENU_INS,    MSG_U(B_INS,"Ins")    },
  54.         { MENUBUT_EDIT,        MENU_EDIT,    MSG_U(B_EDIT,"Edit")    },
  55.         { MENUBUT_RESET,    MENU_RESET,    MSG_U(B_RESET,"Reset")    }
  56.     };
  57.     BUTTON_ALL *ptl = tbl;
  58.     for (unsigned i=0; i<sizeof(tbl)/sizeof(tbl[0]); i++, ptl++){
  59.         if ((ptl->but_id & _but_options) != 0
  60.             && (ptl->but_id != MENUBUT_OK || _but_options == MENUBUT_OK)){
  61.             /* #Specification: dialog / MENUBUT_OK / nerver displayed
  62.                 The button "Ok" is never shown except if it is the
  63.                 only button of the dialog. It is implied by an
  64.                 <enter> on a menu item. Initially it was shown but soon
  65.                 user have started complaining that it was useless.
  66.  
  67.                 From the outside, it looks like a normal buttons, but
  68.                 deep in dialog/buttons.c, it is simply not shown.
  69.             */
  70.             tb[nb] = ptl->str;
  71.             tbret[nb] = ptl->but_ret;
  72.             nb++;
  73.         }
  74.     }
  75.     helpfile.setfrom (_helpfile);
  76.     if (!helpfile.is_empty()){
  77.         tb[nb] = MSG_U(B_HELP,"Help");
  78.         tbret[nb] = MENU_HELP;
  79.         nb++;
  80.     }
  81. }    
  82.  
  83. /*
  84.     Setup the coordinate of each button in the dialog
  85. */
  86. PUBLIC void BUTTONS_INFO::setup(
  87.     int _y,        // Line where the buttons will be drawn
  88.     int width)    // Width of the window
  89. {
  90.     if (nb > 0){
  91.         y = _y;
  92.         int onebut = width/nb;
  93.         for (int i=0; i<nb; i++){
  94.             const char *but = tb[i];
  95.             tbx[i] = i*onebut + (onebut - strlen(but))/2 - 1;
  96.         }
  97.     }
  98. }    
  99.  
  100. /*
  101.     Set the screen cursor on the active button
  102. */
  103. PUBLIC void BUTTONS_INFO::setcursor (
  104.     WINDOW *dialog,
  105.     int button)    // Which buttons is currently selected
  106.             // or -1 if none
  107. {
  108.     if (button != -1 && button < nb){
  109.         int x = tbx[button];
  110.         const char *but = tb[button];
  111.         while (*but == ' '){
  112.             but++;
  113.             x++;
  114.         }
  115.         wmove (dialog,y,x+1);
  116.     }
  117. }
  118.  
  119. /*
  120.     Draw all buttons
  121. */
  122. PUBLIC void BUTTONS_INFO::draw(
  123.     WINDOW *dialog,
  124.     int button)        // Which buttons is currently selected
  125.                 // or -1 if none
  126. {
  127.     for (int i=0; i<nb; i++){
  128.         print_button(dialog,tb[i],y,tbx[i]
  129.             , button == i ? TRUE : FALSE);
  130.     }
  131.     setcursor (dialog,button);
  132. }
  133. PRIVATE void BUTTONS_INFO::help (WINDOW *win)
  134. {
  135.     char path[PATH_MAX];
  136.     sprintf (path,"%s.help",helpfile.get());
  137.     dialog_textbox (path,path);
  138.     touchwin(stdscr);
  139.     touchwin(win);
  140.     dialog_clear ();
  141.     refresh();
  142. }
  143.  
  144. /*
  145.     Process one key.
  146.     This function is called when the focus is in the button section.
  147.     It will process the help button by itself.
  148.  
  149.     It return -1 is nothing special happened. It return the number
  150.     of the selected button otherwise. The called generally
  151.     terminate then.
  152. */
  153. PUBLIC MENU_STATUS BUTTONS_INFO::dokey(
  154.     WINDOW *dialog,
  155.     int key,
  156.     int &selected,
  157.     int other_focus)    // Is there something other that buttons
  158.                 // or something that require specific focus
  159. {
  160.     MENU_STATUS ret = MENU_NULL;
  161.     int button = selected;
  162.     key = toupper(key);
  163.     
  164.     if (key == TAB){
  165.         button++;
  166.         if (button == nb) button = -1;
  167.     }else if (key == KEY_RIGHT){
  168.         button++;
  169.         if (button == nb) button = 0;
  170.     }else if (key == KEY_LEFT){
  171.         button--;
  172.         if (button == -1) button = nb-1;
  173.     }else if (key == '\n'){
  174.         if (!helpfile.is_empty() && button == nb -1){
  175.             help (dialog);
  176.             button = -1;
  177.         }else{
  178.             ret = tbret[button];
  179.         }
  180.     }else if (key == KEY_F(1) && !helpfile.is_empty()){
  181.         help (dialog);
  182.         button = -1;
  183.     }else if (key == 'Y' && (but_options & MENUBUT_YES)){
  184.         ret = MENU_YES;
  185.     }else if (key == 'N' && (but_options & MENUBUT_NO)){
  186.         ret = MENU_NO;
  187.     }
  188.     if (!other_focus && button == -1) button = 0;
  189.     draw(dialog,button);
  190.     selected = button;
  191.     return ret;
  192. }
  193.  
  194.